﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Janus.Cores.Configuration
{
    /// <summary>
    /// 
    /// </summary>
    public sealed class ServiceSection : ConfigurationSection
    {

        /// <summary>
        /// 
        /// </summary>
        [ConfigurationProperty("factory", Options = ConfigurationPropertyOptions.None)]
        public ServiceFactoryElementCollection Factories
        {
            get { return (ServiceFactoryElementCollection)base["factory"]; }

        }
        /// <summary>
        /// 
        /// </summary>
        [ConfigurationProperty("service", Options = ConfigurationPropertyOptions.None)]
        public ServiceFactoryElementCollection Services
        {
            get { return (ServiceFactoryElementCollection)base["service"]; }

        }
    }
    /// <summary>
    /// 
    /// </summary>
    public sealed class ServiceFactoryElement : ConfigurationElement
    {
        /// <summary>
        /// 
        /// </summary>
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get { return (base["name"].ToString()); }
            set { base["name"] = value.ToString(); }
        }

        /// <summary>
        /// 
        /// </summary>
        [ConfigurationProperty("type", IsRequired = true)]
        public string Type
        {
            get { return (base["type"].ToString()); }
            set { base["type"] = value.ToString(); }
        }


        [ConfigurationProperty("lifetime", IsRequired = false)]
        public string LifeTime
        {
            get { return (base["lifetime"].ToString()); }
            set { base["lifetime"] = value.ToString(); }
        }
        
    }

    /// <summary>
    /// 
    /// </summary>
    public sealed class ServiceFactoryElementCollection : ConfigurationElementCollection
    {
        internal const string SECTION_NAME = "factories";
        /// <summary>
        /// 
        /// </summary>
        public ServiceFactoryElementCollection()
        {
            base.AddElementName = "factory";
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new ServiceFactoryElement();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            var el = (element as ServiceFactoryElement);
            return el.Name;
        }
    }
    
    public sealed class ServiceElement : ConfigurationElement
    {
        /// <summary>
        /// 
        /// </summary>
        [ConfigurationProperty("assembly", IsRequired = false)]
        public string Assembly
        {
            get { return (base["assembly"].ToString()); }
            set { base["assembly"] = value.ToString(); }
        }

        /// <summary>
        /// 
        /// </summary>
        [ConfigurationProperty("type", IsRequired = false)]
        public string Type
        {
            get { return (base["type"].ToString()); }
            set { base["type"] = value.ToString(); }
        }

        /// <summary>
        /// 
        /// </summary>
        [ConfigurationProperty("factory", IsRequired = false)]
        public string Factory
        {
            get { return (base["factory"].ToString()); }
            set { base["factory"] = value.ToString(); }
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public sealed class ServiceElementCollection : ConfigurationElementCollection
    {
        internal const string SECTION_NAME = "services";
        
        /// <summary>
        /// 
        /// </summary>
        public ServiceElementCollection()
        {
            base.AddElementName = "service";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new ServiceElement();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            var el = (element as ServiceElement);
            return string.Format("{0},{1}", el.Type,el.Assembly );
        }
    }
}
